package com.bwie.xujiahaonewstitle.activity;
/*
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.newstitlexujiahao.R;
import com.bwie.xujiahaonewstitle.adapter.AddTitleAdapter;
import com.bwie.xujiahaonewstitle.adapter.MyAdapter_two;
import com.bwie.xujiahaonewstitle.fragment.HomeFragment;
import com.bwie.xujiahaonewstitle.httputils.ItemTouchHelperCallback;
import com.bwie.xujiahaonewstitle.httputils.SpaceItemDecoration;

import java.util.ArrayList;*/
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bwie.newstitlexujiahao.R;
import com.bwie.xujiahaonewstitle.View.DragGridView;
import com.bwie.xujiahaonewstitle.View.MyGridView;
import com.bwie.xujiahaonewstitle.adapter.DragAdapter;
import com.bwie.xujiahaonewstitle.adapter.OtherAdapter;

import java.util.ArrayList;
import java.util.List;

public class AddTitleActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
   /* private ArrayList<String> mList;
    private ArrayList<String> mSelectedDatas;
    private ArrayList<String> mUnselectedDatas;
    private TextView mFinish;
    private RecyclerView mRecycleSelected,mRecycleUnSelected;
    private AddTitleAdapter mAddTitleAdapter;
    private MyAdapter_two mMyAdapter_two;
    private ImageView mMain3_iv;
    private ItemTouchHelper mItemTouchHelper;
    private TextView mHideorshow;
    private boolean isDeleteIconsShow = false;*/
    private MyGridView mOtherGv;
    private DragGridView mUserGv;
    private List<String> mOtherList = new ArrayList<>();
    private List alist;
    private DragAdapter  mUserAdapter;
    private OtherAdapter mOtherAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_title);
        alist = (List<Object>)getIntent().getSerializableExtra("key");
        Log.d("接收数据", alist.toString());
        initView();
       /* Intent mIntent = getIntent();
        mList = mIntent.getStringArrayListExtra("key");
        initData();
        //初始化控件的方法
        initView();*/
    }
    private void initView() {
        mUserGv = (DragGridView) findViewById(R.id.userGridView);
        mOtherGv = (MyGridView) findViewById(R.id.otherGridView);
        mOtherList.add("汽车");
        mOtherList.add("房产");
        mOtherList.add("社会");
        mOtherList.add("情感");
        mOtherList.add("女人");
        mOtherList.add("旅游");
        mOtherList.add("健康");
        mOtherList.add("美女");
        mOtherList.add("游戏");
        mOtherList.add("数码");
        mOtherList.add("娱乐");
        mOtherList.add("探索");
        mUserAdapter = new DragAdapter(this, alist,true);
        mOtherAdapter = new OtherAdapter(this, mOtherList,false);
        mUserGv.setAdapter(mUserAdapter);
        mOtherGv.setAdapter(mOtherAdapter);
        mUserGv.setOnItemClickListener(this);
        mOtherGv.setOnItemClickListener(this);
    }
    /**
     *获取点击的Item的对应View，
     *因为点击的Item已经有了自己归属的父容器MyGridView，所有我们要是有一个ImageView来代替Item移动
     * @param view
     * @return
     */
    private ImageView getView(View view) {
        view.destroyDrawingCache();
        view.setDrawingCacheEnabled(true);
        Bitmap cache = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        ImageView iv = new ImageView(this);
        iv.setImageBitmap(cache);
        return iv;
    }
    /**
     * 获取移动的VIEW，放入对应ViewGroup布局容器
     * @param viewGroup
     * @param view
     * @param initLocation
     * @return
     */
    private View getMoveView(ViewGroup viewGroup, View view, int[] initLocation) {
        int x = initLocation[0];
        int y = initLocation[1];
        viewGroup.addView(view);
        LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mLayoutParams.leftMargin = x;
        mLayoutParams.topMargin = y;
        view.setLayoutParams(mLayoutParams);
        return view;
    }

    /**
     * 创建移动的ITEM对应的ViewGroup布局容器
     * 用于存放我们移动的View
     */
    private ViewGroup getMoveViewGroup() {
        //window中最顶层的view
        ViewGroup moveViewGroup = (ViewGroup) getWindow().getDecorView();
        LinearLayout moveLinearLayout = new LinearLayout(this);
        moveLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        moveViewGroup.addView(moveLinearLayout);
        return moveLinearLayout;
    }
    /**
     * 点击ITEM移动动画
     *
     * @param moveView
     * @param startLocation
     * @param endLocation
     * @param moveChannel
     * @param clickGridView
     */
    private void MoveAnim(View moveView, int[] startLocation, int[] endLocation, final String moveChannel,
                          final GridView clickGridView, final boolean isUser) {
        int[] initLocation = new int[2];
        //获取传递过来的VIEW的坐标
        moveView.getLocationInWindow(initLocation);
        //得到要移动的VIEW,并放入对应的容器中
        final ViewGroup moveViewGroup = getMoveViewGroup();
        final View mMoveView = getMoveView(moveViewGroup, moveView, initLocation);
        //创建移动动画
        TranslateAnimation moveAnimation = new TranslateAnimation(
                startLocation[0], endLocation[0], startLocation[1],
                endLocation[1]);
        moveAnimation.setDuration(300L);//动画时间
        //动画配置
        AnimationSet moveAnimationSet = new AnimationSet(true);
        moveAnimationSet.setFillAfter(false);//动画效果执行完毕后，View对象不保留在终止的位置
        moveAnimationSet.addAnimation(moveAnimation);
        mMoveView.startAnimation(moveAnimationSet);
        moveAnimationSet.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                moveViewGroup.removeView(mMoveView);
                // 判断点击的是DragGrid还是OtherGridView
                if (isUser) {
                    mOtherAdapter.setVisible(true);
                    mOtherAdapter.notifyDataSetChanged();
                    mUserAdapter.remove();
                } else {
                    mUserAdapter.setVisible(true);
                    mUserAdapter.notifyDataSetChanged();
                    mOtherAdapter.remove();
                }
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
        switch (parent.getId()){
            case R.id.userGridView:
                //position为 0，1 的不可以进行任何操作
                if (position != 0 && position != 1) {
                    final ImageView moveImageView = getView(view);
                    if (moveImageView != null) {
                        TextView newTextView = (TextView) view.findViewById(R.id.text_item);
                        final int[] startLocation = new int[2];
                        newTextView.getLocationInWindow(startLocation);
                        final String channel = ((DragAdapter) parent.getAdapter()).getItem(position);//获取点击的频道内容
                        mOtherAdapter.setVisible(false);
                        //添加到最后一个
                        mOtherAdapter.addItem(channel);
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                try {
                                    int[] endLocation = new int[2];
                                    //获取终点的坐标
                                    mOtherGv.getChildAt(mOtherGv.getLastVisiblePosition()).getLocationInWindow(endLocation);
                                    MoveAnim(moveImageView, startLocation, endLocation, channel, mUserGv, true);
                                    mUserAdapter.setRemove(position);
                                } catch (Exception localException) {
                                }
                            }
                        }, 50L);
                    }
                }
                break;
            case R.id.otherGridView:
                final ImageView moveImageView = getView(view);
                if (moveImageView != null) {
                    TextView newTextView = (TextView) view.findViewById(R.id.text_item);
                    final int[] startLocation = new int[2];
                    newTextView.getLocationInWindow(startLocation);
                    final String channel = ((OtherAdapter) parent.getAdapter()).getItem(position);
                    mUserAdapter.setVisible(false);
                    //添加到最后一个
                    mUserAdapter.addItem(channel);
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            try {
                                int[] endLocation = new int[2];
                                //获取终点的坐标
                                mUserGv.getChildAt(mUserGv.getLastVisiblePosition()).getLocationInWindow(endLocation);
                                MoveAnim(moveImageView, startLocation, endLocation, channel, mOtherGv,false);
                                mOtherAdapter.setRemove(position);
                            } catch (Exception localException) {
                            }
                        }
                    }, 50L);
                }
                break;
            default:
                break;

        }
    }

   /* public boolean isDeleteIconsShow() {
        return isDeleteIconsShow;
    }
    private void initView() {
        mFinish = (TextView) findViewById(R.id.finish);
        mRecycleSelected = (RecyclerView) findViewById(R.id.top_recycler);
        mRecycleUnSelected = (RecyclerView) findViewById(R.id.bottom_recycler);
        //创建第一个RecyclerView的初始化
        mRecycleSelected.setLayoutManager(new GridLayoutManager(this,4));
        mAddTitleAdapter = new AddTitleAdapter(mSelectedDatas,this);
        mRecycleSelected.setAdapter(mAddTitleAdapter);
        //调用此方法来对子item项进行修饰
        mRecycleSelected.addItemDecoration(new SpaceItemDecoration(8));
        //第二个RecyclerView的初始化
        mRecycleUnSelected.setLayoutManager(new GridLayoutManager(this, 4));
        mMyAdapter_two = new MyAdapter_two(this, mUnselectedDatas);
        mRecycleUnSelected.setAdapter(mMyAdapter_two);
        mRecycleUnSelected.addItemDecoration(new SpaceItemDecoration(8));
        mMain3_iv = (ImageView) findViewById(R.id.main3_iv);
        mMain3_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mHideorshow = (TextView) findViewById(R.id.hideorshow);
    }

    private void initData() {

        mSelectedDatas = new ArrayList<String>();
        mSelectedDatas.addAll(mList);
        //获取名字存入mUnselectedDatas集合中
        mUnselectedDatas = new ArrayList<String>();
        for (int i = 0; i < HomeFragment.mData.size(); i++) {
            mUnselectedDatas.add(HomeFragment.mData.get(i).getName());
        }
    }

    private void initEvent() {
        //实例化ItemTouchHelperCallback,与回调接口有关可以结合MainActivity,MyAdapter,ItemTouchHelperCallback一起理解
        ItemTouchHelperCallback callback = new ItemTouchHelperCallback(mAddTitleAdapter);
        //ItemTouchHelper item项触摸帮助类不理解的可以自主学习
        mItemTouchHelper = new ItemTouchHelper(callback);
        //绑定RecyclerView
        mItemTouchHelper.attachToRecyclerView(mRecycleSelected);
        //MyAdapter里的公共构造方法
        mAddTitleAdapter.setOnItemClickListener(new AddTitleAdapter.OnItemClickListener() {
            //点击监听
            @Override
            public void onItemClickListener(AddTitleAdapter.MyViewHolder viewHolder, int pos) {
                if (!isDeleteIconsShow) {
                    Toast.makeText(AddTitleActivity.this, mSelectedDatas.get(pos), Toast.LENGTH_SHORT).show();
                }
            }

            //长按item监听
            @Override
            public void onItemLongClickListener(AddTitleAdapter.MyViewHolder viewHolder, int pos) {
                if (!isDeleteIconsShow) {
                    showAllDeleteIcons();
                    mFinish.setText("  完成  ");
                    mHideorshow.setVisibility(View.VISIBLE);
                }
            }
        });
        //点击切换栏目就隐藏所有小叉号
        mFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideAllDeleteIcons();
                mFinish.setVisibility(View.INVISIBLE);
                mHideorshow.setVisibility(View.INVISIBLE);
            }
        });
        //点击删除item,跟下面的同理
        mAddTitleAdapter.setOnDeleteIconClickListener(new AddTitleAdapter.OnDeleteIconClickListener() {
            @Override
            public void onDeleteIconClick(int pos) {
                mMyAdapter_two.addData(mSelectedDatas.get(pos), mUnselectedDatas.size());
                mAddTitleAdapter.removeData(pos);
            }
        });
        //点击下面的RecyclerView时点那个item就把那个item添加到上面的RecyclerView中去,并且删除下面的RecyclerView中相应的item项
        mMyAdapter_two.setOnitemClickListener(new MyAdapter_two.OnItemClickListener() {
            @Override
            public void onItemClick(MyAdapter_two.ViewHolder holder, int pos) {
                mAddTitleAdapter.addData(mUnselectedDatas.get(pos), mSelectedDatas.size());
                mMyAdapter_two.removeData(pos);
            }
        });
    }

    private void showAllDeleteIcons() {
        //与hideAllDeleteIcons同理
        int count = mRecycleSelected.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = mRecycleSelected.getChildAt(i);
            ImageView delete = (ImageView) child.findViewById(R.id.delelte);
            delete.setVisibility(View.VISIBLE);
        }
        isDeleteIconsShow = true;
    }

 *//*
     * 隐藏所有的删除图标
*//*
    private void hideAllDeleteIcons() {
        //获得此RecyclerView的所有子条目
        int count = mRecycleSelected.getChildCount();
        for (int i = 0; i < count; i++) {
            //获得子布局
            View child = mRecycleSelected.getChildAt(i);
            //找到小叉号图片的ID
            ImageView delete = (ImageView) child.findViewById(R.id.delelte);
            //设置不可见
            delete.setVisibility(View.INVISIBLE);
        }
        //将isDeleteIconsShow重新赋值
        isDeleteIconsShow = false;
    }
*/
}
